﻿Imports System.IO
Imports System.Globalization

Partial Public Class Mship
    Inherits System.Web.UI.Page

    Dim clsGet As New ClassFunctionGetData
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim lbPage As Label = CType(Master.FindControl("lbPage"), Label)
            lbPage.Text = "Monthly Ship Report"
            For i As Integer = 0 To 4
                With ddlYear
                    .Items.Insert(i, Date.Now.AddYears(i - 2).Year)
                    .Items(i).Value = Date.Now.AddYears(i - 2).Year
                End With
            Next
            ddlYear.SelectedIndex = 2
            If Not (ddlMonth.Items.FindByValue(Date.Now.Month) Is Nothing) Then
                ddlMonth.SelectedValue = Date.Now.Month
            End If

        End If
    End Sub
    Private Sub MainPage_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
        Dim ds As DataSet = Session("UserData")
        If Not (ds Is Nothing) Then
            Page.MasterPageFile = clsGet.GetMaster_Page(ds.Tables(0).Rows(0).Item("User_Name").ToString(), "M")
        End If
    End Sub

    Private Sub btnPrint_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPrint.Click
        Dim service As New _ReportingService.Report

        Dim optWindow As String = "left=0,top=0,location=no,menubar=no,resizable=yes,scrollbars=yes,status=yes,toolbar=no"
        Dim Orientation As Boolean = True
        Dim ReportName As String = String.Empty
        Dim reportDataSource As String = String.Empty
        Dim paraName As String = String.Empty
        Dim paraValue As String = String.Empty
        Dim reportType As String = String.Empty
        Dim urlDisplay As String = String.Empty
        Dim dsReport As New DataSet
        Dim Product_in As Double = 0.0
        Dim Product_out As Double = 0.0
        dsReport.Tables.Add(Get_Value(Product_in, Product_out))
        dsReport.AcceptChanges()

        ReportName = "Mship.rdlc"
        ' dsReport = New DataSet
        reportDataSource = "Mship_Mship"
        paraName = "Month;Product_IN;Product_Out;DateTimeNow"
        paraValue = ddlMonth.SelectedItem.Text & " " & CInt(ddlYear.SelectedValue) & ";" & Product_in & ";" & Product_out & ";" & DateTime.Now.ToString()
        Orientation = False
        reportType = radioType.SelectedValue

        urlDisplay = service.CreateReporting(dsReport, reportDataSource, reportType, ReportName, paraName, paraValue, Orientation, "")

        Dim oldFile As String = String.Empty
        oldFile = Server.MapPath(urlDisplay)
        Dim fileInfo As FileInfo = New FileInfo(oldFile)
        Dim reportPath As String = "../WriteReport/"

        Dim newFileName As String = DateTime.Now.ToString("_yyMMdd_HHmmss", New CultureInfo("en-US"))
        newFileName = "Mship_Report" & newFileName

        If fileInfo.Exists Then
            fileInfo.MoveTo(fileInfo.DirectoryName & "/" & newFileName & fileInfo.Extension)
            urlDisplay = reportPath & newFileName & fileInfo.Extension
        End If

        ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "window.open('" + urlDisplay + "','_blank','" + optWindow + "');", True)
    End Sub

    Private Function Get_Value(ByRef Product_In As Double, ByRef Product_Out As Double) As DataTable
        Dim dtLiquid As DataTable = clsGet.Get_MshipLiquid(ddlYear.SelectedValue & CInt(ddlMonth.SelectedValue).ToString("00") & "00", ddlYear.SelectedValue & CInt(ddlMonth.SelectedValue).ToString("00") & "32")
        Dim dtBulk As DataTable = clsGet.Get_MshipBulk(ddlYear.SelectedValue & CInt(ddlMonth.SelectedValue).ToString("00") & "00", ddlYear.SelectedValue & CInt(ddlMonth.SelectedValue).ToString("00") & "32")
        Dim dtValue As DataTable = create()
        Dim C_V_1 As Integer = 0
        Dim C_M_1 As Double = 0
        Dim C_V_2 As Integer = 0
        Dim C_M_2 As Double = 0
        Dim C_V_3 As Integer = 0
        Dim C_M_3 As Double = 0
        Dim C_V_4 As Integer = 0
        Dim C_M_4 As Double = 0
        Dim C_V_5 As Integer = 0
        Dim C_M_5 As Double = 0
        Dim LC_V_1 As Integer = 0
        Dim LC_M_1 As Double = 0
        Dim LC_3_1 As Double = 0
        Dim LC_V_2 As Integer = 0
        Dim LC_M_2 As Double = 0
        Dim LC_3_2 As Double = 0
        Dim LC_V_3 As Integer = 0
        Dim LC_M_3 As Double = 0
        Dim LC_3_3 As Double = 0
        Dim LC_V_4 As Integer = 0
        Dim LC_M_4 As Double = 0
        Dim LC_3_4 As Double = 0
        Dim LC_V_5 As Integer = 0
        Dim LC_M_5 As Double = 0
        Dim LC_3_5 As Double = 0

        Dim Grt As Double = 0
        Dim MT As Double = 0
        Dim M3 As Double = 0

        Dim Booking_Old As String = ""
        For Each dr As DataRow In dtBulk.Rows
            Grt = CDbl(dr.Item("Vehicle_GRT").ToString.Trim())
            MT = CDbl(dr.Item("_M").ToString.Trim())
            Dim dtProductBulk As DataTable = clsGet.Get_Load_Unload_Product(dr.Item("Booking_no"), "")
            MT = 0
            For Each dm As DataRow In dtProductBulk.Rows
                'If (dm.Item("BU_Truck") & "" = "") Then
                '    MT += CDbl(dm.Item("BU_Bill"))
                'Else
                '    MT += CDbl(dm.Item("BU_Truck"))
                'End If
                Try
                    If (dm.Item("BU_Bill") & "" = "") Then
                        MT += dm.Item("BU_Manifest")
                    Else
                        MT += dm.Item("BU_Bill")
                    End If
                Catch ex As Exception

                End Try

            Next
            Dim Shifting As Boolean = False
            Dim BookingShifting As String = ""
            Dim dtWharf As DataTable = clsGet.Get_BookingWharf(dr.Item("Booking_no"))
            If (dtWharf.Rows.Count > 0) Then
                If (dtWharf.Rows(0).Item("Shift_Booking") & "" <> "") Then
                    Shifting = True
                    BookingShifting = dtWharf.Rows(0).Item("Shift_Booking")
                End If
            End If
            If (Shifting) Then
                Dim dtProduct As DataTable = clsGet.Get_Load_Unload_Product(BookingShifting, "")
                For Each dm As DataRow In dtProduct.Rows
                    Try
                        If (dm.Item("BU_Bill") & "" = "") Then
                            MT += dm.Item("BU_Manifest")
                        Else
                            MT += dm.Item("BU_Bill")
                        End If
                    Catch ex As Exception

                    End Try
                Next
            End If
            If (Grt < 500.0) Then
                C_V_1 += 1
                C_M_1 += MT
            ElseIf (Grt < 2999.0) Then
                C_V_2 += 1
                C_M_2 += MT
            ElseIf (Grt < 6999.0) Then
                C_V_3 += 1
                C_M_3 += MT
            ElseIf (Grt < 9999.0) Then
                C_V_4 += 1
                C_M_4 += MT
            Else
                C_V_5 += 1
                C_M_5 += MT
            End If
        Next

        Dim BOoking_no As String = ""

        If (dtLiquid.Rows.Count > 0) Then
            BOoking_no = dtLiquid.Rows(0).Item("Booking_no")
            Dim Record As DataTable = clsGet.Get_BookingOneRecord(BOoking_no).Tables(0)
            Grt = CDbl(Record.Rows(0).Item("Vehicle_GRT").ToString.Trim())
            MT = 0
            M3 = 0
        End If
        Dim C As Integer = 0
        Dim checkAdd As Boolean = True
        For Each dr As DataRow In dtLiquid.Rows
            C += 1
            If ((BOoking_no <> dr.Item("Booking_no")) Or (C = dtLiquid.Rows.Count)) Then
                BOoking_no = dr.Item("Booking_no")
                Dim Record As DataTable = clsGet.Get_BookingOneRecord(dr.Item("Booking_no")).Tables(0)
                If (Record.Rows(0).Item("Vehicle_Destination") & "" = "I") Then
                    Try
                        Product_In += MT
                    Catch ex As Exception

                    End Try
                Else
                    Try
                        Product_Out += MT
                    Catch ex As Exception

                    End Try
                End If
                If (Grt < 500.0) Then
                    LC_V_1 += 1
                    LC_M_1 += MT
                    LC_3_1 += M3
                    checkAdd = True
                ElseIf (Grt < 2999.0) Then
                    LC_V_2 += 1
                    LC_M_2 += MT
                    LC_3_2 += M3
                    checkAdd = True
                ElseIf (Grt < 6999.0) Then
                    LC_V_3 += 1
                    LC_M_3 += MT
                    LC_3_3 += M3
                    checkAdd = True
                ElseIf (Grt < 9999.0) Then
                    LC_V_4 += 1
                    LC_M_4 += MT
                    LC_3_4 += M3
                    checkAdd = True
                Else
                    LC_V_5 += 1
                    LC_M_5 += MT
                    LC_3_5 += M3
                    checkAdd = True
                End If



                Grt = CDbl(Record.Rows(0).Item("Vehicle_GRT").ToString.Trim())
                Try
                    MT = CDbl(dr.Item("LU_BLMT").ToString.Trim())
                    checkAdd = False
                Catch ex As Exception
                    MT = 0
                End Try
                Try
                    M3 = CDbl(dr.Item("LU_BL").ToString.Trim())
                    checkAdd = False
                Catch ex As Exception
                    M3 = 0
                End Try
                Dim Shifting As Boolean = False
                Dim BookingShifting As String = ""
                Dim dtWharf As DataTable = clsGet.Get_BookingWharf(BOoking_no)
                If (dtWharf.Rows.Count > 0) Then
                    If (dtWharf.Rows(0).Item("Shift_Booking") & "" <> "") Then
                        Shifting = True
                        BookingShifting = dtWharf.Rows(0).Item("Shift_Booking")
                    End If
                End If
                If (Shifting) Then
                    Dim dtProduct As DataTable = clsGet.Get_Load_Unload_Product(BookingShifting, "")
                    For Each dm As DataRow In dtProduct.Rows
                        Try
                            MT += CDbl(dm.Item("LU_BLMT").ToString.Trim())
                            checkAdd = False
                        Catch ex As Exception

                        End Try
                        Try
                            M3 += CDbl(dm.Item("LU_BL").ToString.Trim())
                            checkAdd = False
                        Catch ex As Exception

                        End Try

                    Next
                End If
            Else
                Try
                    MT += CDbl(dr.Item("LU_BLMT").ToString.Trim())
                    checkAdd = False
                Catch ex As Exception

                End Try
                Try
                    M3 += CDbl(dr.Item("LU_BL").ToString.Trim())
                    checkAdd = False
                Catch ex As Exception

                End Try
                Dim Shifting As Boolean = False
                Dim BookingShifting As String = ""
                Dim dtWharf As DataTable = clsGet.Get_BookingWharf(BOoking_no)
                If (dtWharf.Rows.Count > 0) Then
                    If (dtWharf.Rows(0).Item("Shift_Booking") & "" <> "") Then
                        Shifting = True
                        BookingShifting = dtWharf.Rows(0).Item("Shift_Booking")
                    End If
                End If
                If (Shifting) Then
                    Dim dtProduct As DataTable = clsGet.Get_Load_Unload_Product(BookingShifting, "")
                    For Each dm As DataRow In dtProduct.Rows
                        Try
                            MT += CDbl(dm.Item("LU_BLMT").ToString.Trim())
                            checkAdd = False
                        Catch ex As Exception

                        End Try
                        Try
                            M3 += CDbl(dm.Item("LU_BL").ToString.Trim())
                            checkAdd = False
                        Catch ex As Exception

                        End Try

                    Next
                End If
            End If

        Next
        If Not (checkAdd) Then
            If (Grt < 500.0) Then
                LC_V_1 += 1
                LC_M_1 += MT
                LC_3_1 += M3
                checkAdd = True
            ElseIf (Grt < 2999.0) Then
                LC_V_2 += 1
                LC_M_2 += MT
                LC_3_2 += M3
                checkAdd = True
            ElseIf (Grt < 6999.0) Then
                LC_V_3 += 1
                LC_M_3 += MT
                LC_3_3 += M3
                checkAdd = True
            ElseIf (Grt < 9999.0) Then
                LC_V_4 += 1
                LC_M_4 += MT
                LC_3_4 += M3
                checkAdd = True
            Else
                LC_V_5 += 1
                LC_M_5 += MT
                LC_3_5 += M3
                checkAdd = True
            End If
        End If
        For i As Integer = 0 To 7
            Select Case (i)
                Case 0
                    Dim dr As DataRow = dtValue.NewRow
                    dr.Item("M_Text") = "ต่ำกว่า ๕oo"
                    dr.Item("B_Vessel") = C_V_1
                    dr.Item("B_MT") = C_M_1
                    dr.Item("L_Vessel") = LC_V_1
                    dr.Item("L_MT") = LC_M_1
                    dr.Item("L_M3") = LC_3_1
                    dtValue.Rows.Add(dr)
                    dtValue.AcceptChanges()
                Case 1
                    Dim dr As DataRow = dtValue.NewRow
                    dr.Item("M_Text") = "๕oo - ๒,๙๙๙"
                    dr.Item("B_Vessel") = C_V_2
                    dr.Item("B_MT") = C_M_2
                    dr.Item("L_Vessel") = LC_V_2
                    dr.Item("L_MT") = LC_M_2
                    dr.Item("L_M3") = LC_3_2
                    dtValue.Rows.Add(dr)
                    dtValue.AcceptChanges()
                Case 2
                    Dim dr As DataRow = dtValue.NewRow
                    dr.Item("M_Text") = "๒,๙๙๙ - ๖,๙๙๙"
                    dr.Item("B_Vessel") = C_V_3
                    dr.Item("B_MT") = C_M_3
                    dr.Item("L_Vessel") = LC_V_3
                    dr.Item("L_MT") = LC_M_3
                    dr.Item("L_M3") = LC_3_3
                    dtValue.Rows.Add(dr)
                    dtValue.AcceptChanges()
                Case 3
                    Dim dr As DataRow = dtValue.NewRow
                    dr.Item("M_Text") = "๖,๙๙๙ - ๙,๙๙๙"
                    dr.Item("B_Vessel") = C_V_4
                    dr.Item("B_MT") = C_M_4
                    dr.Item("L_Vessel") = LC_V_4
                    dr.Item("L_MT") = LC_M_4
                    dr.Item("L_M3") = LC_3_4
                    dtValue.Rows.Add(dr)
                    dtValue.AcceptChanges()
                Case 4
                    Dim dr As DataRow = dtValue.NewRow
                    dr.Item("M_Text") = "๑o,ooo ขึ้นไป"
                    dr.Item("B_Vessel") = C_V_5
                    dr.Item("B_MT") = C_M_5
                    dr.Item("L_Vessel") = LC_V_5
                    dr.Item("L_MT") = LC_M_5
                    dr.Item("L_M3") = LC_3_5
                    dtValue.Rows.Add(dr)
                    dtValue.AcceptChanges()
            End Select
        Next
        Return dtValue

    End Function

    Private Function create() As DataTable
        Dim dt As New DataTable
        dt.Columns.Add("M_Text", GetType(String))
        dt.Columns.Add("B_Vessel", GetType(String))
        dt.Columns.Add("B_MT", GetType(String))
        dt.Columns.Add("L_Vessel", GetType(String))
        dt.Columns.Add("L_MT", GetType(String))
        dt.Columns.Add("L_M3", GetType(String))
        dt.AcceptChanges()
        Return dt
    End Function
End Class